<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生邮箱" prop="stuEmail">
        <el-input
          v-model="queryParams.stuEmail"
          placeholder="请输入学生邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生微信" prop="stuWechat">
        <el-input
          v-model="queryParams.stuWechat"
          placeholder="请输入学生微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生电话" prop="stuPhone">
        <el-input
          v-model="queryParams.stuPhone"
          placeholder="请输入学生电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="学生班级" prop="classes">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.classes"-->
      <!--          placeholder="请输入学生班级"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="提问时间" prop="questionTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.questionTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择提问时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="老师姓名" prop="teacherName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.teacherName"-->
      <!--          placeholder="请输入老师姓名"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['question:que:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['question:que:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['question:que:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['question:que:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="queList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="学生提问id" align="center" prop="id" />-->
      <el-table-column label="学生姓名" align="center" prop="stuName"/>
      <el-table-column label="学生邮箱" align="center" prop="stuEmail"/>
      <el-table-column label="学生微信" align="center" prop="stuWechat"/>
      <el-table-column label="学生电话" align="center" prop="stuPhone"/>
      <el-table-column label="学生班级" align="center" prop="classes"/>
      <el-table-column label="提问时间" align="center" prop="questionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.questionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="problemType"/>
      <el-table-column label="老师姓名" align="center" prop="teacherName"/>
      <el-table-column label="问题描述" align="center" prop="problem"/>
      <!--      <el-table-column label="问题状态  0-待解决 1-已解决" align="center" prop="status" />-->
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <el-tag type="danger" effect="dark" v-if="scope.row.status===0">待解决</el-tag>
          <el-tag type="success" effect="dark" v-if="scope.row.status===1">已解决</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="transferShow(scope.row)"
          >移交
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleResolve(scope.row)"
          >解决
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['question:que:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['question:que:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问题解决对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生姓名"/>
        </el-form-item>
        <el-form-item label="学生邮箱" prop="stuEmail">
          <el-input v-model="form.stuEmail" placeholder="请输入学生邮箱"/>
        </el-form-item>
        <el-form-item label="学生微信" prop="stuWechat">
          <el-input v-model="form.stuWechat" placeholder="请输入学生微信"/>
        </el-form-item>
        <el-form-item label="学生电话" prop="stuPhone">
          <el-input v-model="form.stuPhone" placeholder="请输入学生电话"/>
        </el-form-item>
        <el-form-item label="学生班级" prop="classes">
          <el-input v-model="form.classes" placeholder="请输入学生班级"/>
        </el-form-item>
        <el-form-item label="提问时间" prop="questionTime">
          <el-date-picker clearable
                          v-model="form.questionTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择提问时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型" prop="problemType">
          <el-input v-model="form.problemType" placeholder="请输入问题类型"/>
        </el-form-item>
        <el-form-item label="老师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入老师姓名"/>
        </el-form-item>
        <el-form-item label="问题描述" prop="problem">
          <el-input v-model="form.problem" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


<!--    &lt;!&ndash;    移交对话框&ndash;&gt;-->
<!--    <el-dialog title="移交问题" :visible.sync="transferDialogVisible" width="500px" append-to-body>-->
<!--      <el-form ref="transferForm" :model="transferForm" label-width="80px">-->
<!--        <el-form-item label="选择老师" prop="teacher">-->
<!--          <el-select v-model="transferForm.teacher" placeholder="请选择老师">-->
<!--            <el-option v-for="option in teachers" :key="option.id" :label="option.name" :value="option.id"/>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="移交理由" prop="reason">-->
<!--          <el-input v-model="transferForm.reason" type="textarea" autosize placeholder="请输入移交理由"/>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <span slot="footer" class="dialog-footer">-->
<!--      <el-button @click="cancelTransfer">取消</el-button>-->
<!--      <el-button type="primary" @click="confirmTransfer">确定</el-button>-->
<!--    </span>-->
<!--    </el-dialog>-->

  </div>
</template>

<script>
import {listQue, getQue, delQue, addQue, updateQue, resolveProblem, transferQue} from "@/api/question/que";


export default {
  name: "Que",
  data() {
    return {
      transferDialogVisible: false,
      transferForm: {
        teacher: null,
        reason: ''
      },
      teachers: [
        {id: 1, name: '张老师'},
        {id: 2, name: '李老师'},
        {id: 3, name: '王老师'}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题解决表格数据
      queList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
        stuEmail: null,
        stuWechat: null,
        stuPhone: null,
        classes: null,
        questionTime: null,
        problemType: null,
        teacherName: null,
        problem: null,
        status: null
      },
      options: [{
        id: 1,
        label: 'Java'
      }, {
        id: 2,
        label: 'Python'
      }, {
        id: 3,
        label: 'C#'
      }],
      value: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询问题解决列表 */
    getList() {
      this.loading = true;
      listQue(this.queryParams).then(response => {
        this.queList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuName: null,
        stuEmail: null,
        stuWechat: null,
        stuPhone: null,
        classes: null,
        questionTime: null,
        problemType: null,
        teacherName: null,
        problem: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题解决";
    },
    // // 显示移交对话框，并将当前行数据传递给对话框组件
    // transferShow(row) {
    //   // 处理传入的行数据
    //   // ...
    //   this.transferData = row
    //   this.transferDialogVisible = true;
    // },
    // cancelTransfer() {
    //   this.transferDialogVisible = false;
    //   // 清空表单数据
    //   this.$refs.transferForm.resetFields();
    // },
    // confirmTransfer() {
    //   this.$refs.transferForm.validate(valid => {
    //     if (valid) {
    //       // 获取选中的问题解决id
    //       const id = this.ids;
    //       // 构造移交参数
    //       const transferData = {
    //         queId: id,
    //         teacherName: this.transferData.teacher,
    //       };
    //       // 调用接口实现移交操作
    //       transferQue(transferData).then(response => {
    //         this.$message.success("问题移交成功");
    //         this.transferDialogVisible = false;
    //         // 清空表单数据
    //         this.$refs.transferForm.resetFields();
    //         // 刷新问题解决列表
    //         this.getList();
    //       }).catch(error => {
    //         this.$message.error("问题移交失败，请重试");
    //       });
    //       this.transferDialogVisible = false;
    //       // 清空表单数据
    //       this.$refs.transferForm.resetFields();
    //     }
    //   });
    // },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQue(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题解决";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 解决问题 */
    handleResolve(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确定解决为"' + ids + '"的问题?').then(function () {
        return resolveProblem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("问题处理成功");
      }).catch(() => {
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题解决编号为"' + ids + '"的数据项？').then(function () {
        return delQue(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('question/que/export', {
        ...this.queryParams
      }, `que_${new Date().getTime()}.xlsx`)
    }
  }
}
;
</script>
